package com.zxbg.controller.systemtoolsController;

import com.zxbg.controller.Code;
import com.zxbg.controller.Result;
import com.zxbg.domain.SystemtoolsLeftMenu;
import com.zxbg.domain.Tenant;
import com.zxbg.service.impl.systemtoolsServiceImpl.RoleManagementServiceImpl;
import com.zxbg.service.systemtoolsService.IndexService;
import com.zxbg.service.systemtoolsService.RoleManagementService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;

@RestController
@RequestMapping("/systemtoolsindex")
public class indexController {
    @Autowired
    private IndexService indexService;
    @Autowired
    private RoleManagementService roleManagementService;
    @Autowired
    private RoleManagementServiceImpl roleManagementServiceImpl;

    @GetMapping("/menu")
    public Result getLeftMenu(@RequestHeader Integer tenant_id, @RequestHeader String employee_id) {
        boolean ifSysadmin = roleManagementService.getIfSysadmin(employee_id, tenant_id);
        Map<String, Set> allAuth = new HashMap<>();
        if(!ifSysadmin) {
            allAuth = roleManagementService.getAllAuth(employee_id, tenant_id);
        } else {
            allAuth.put("menu", null);
        }
        List<SystemtoolsLeftMenu> systemtoolsLeftMenus = indexService.getLeftMenu("", tenant_id, allAuth.get("menu"), ifSysadmin);
        Map<String, Object> res = new HashMap<>();
        res.put("menu", systemtoolsLeftMenus);
        res.put("authority", roleManagementServiceImpl.getIfSysadmin(employee_id, tenant_id) ? "all" : allAuth.get("authority"));
        return new Result(Code.GET_OK, res);
    }

    @PostMapping("/count")
    public Result getCount(@RequestBody String tenant_id) {
        return new Result(Code.GET_OK, indexService.getCount(Integer.parseInt(tenant_id)));
    }

    @PostMapping("/base")
    public Result getBase(@RequestBody String tenant_id) {
        Tenant tenant = indexService.getBase(Integer.parseInt(tenant_id));
        return new Result(Code.GET_OK, tenant);
    }

    @GetMapping("/getAllMenuCode")
    public Result getAllMenuCode(@RequestHeader Integer tenant_id) {
        return new Result(Code.GET_OK, indexService.getAllMenuCode(tenant_id));
    }
}
